package icegps.com.pingdj.activity.settingactivity;

import android.view.View;
import android.widget.TextView;

import butterknife.BindView;
import icegps.com.pingdj.R;
import icegps.com.pingdj.activity.BaseActivity;
import icegps.com.pingdj.bean.DeviceStatus;
import icegps.com.pingdj.bean.LocationStatus;
import icegps.com.pingdj.data.ble.WriteHelper;
import icegps.com.pingdj.map.thread.ThreadPool;
import icegps.com.pingdj.utils.StringUtils;
import icegps.com.pingdj.utils.UnitUtils;
import icegps.com.pingdj.view.UnitTextView;
import icegps.com.pingdj.view.ValueTextView;

/**
 * Created by 111 on 2018/3/20.
 */

public class BalanceCalibrationActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.tv_ins_mode)
    TextView tvInsMode;
    @BindView(R.id.tv_set_value)
    ValueTextView tvSetValue;
    @BindView(R.id.tv_current_value)
    ValueTextView tvCurrentValue;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.tv_hint)
    TextView tvHint;
    @BindView(R.id.tv_plane_set_value_unit)
    UnitTextView tvSetValueUnit;
    @BindView(R.id.tv_plane_height_unit)
    UnitTextView tvCurrentValueUnit;
    private int step;
    private double balanceDifference;
    @Override
    protected String setTitle() {
        return getString(R.string.balance_calibration);
    }

    @Override
    protected void init() {
        setThisStatus();
        tvConfirm.setOnClickListener(this);
        ThreadPool.getInstance().executeFixed(new Runnable() {
            @Override
            public void run() {
                double setValue = deviceStatus.getSetValue();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        setSetBalance(setValue);
                    }
                });
            }
        });


    }

    private void setThisStatus() {
        switch (deviceStatus.getInsMode()) {
            case 0:
                tvInsMode.setText(R.string.ins_one);
                break;
            case 1:
                tvInsMode.setText(R.string.ins_two);
                break;
            case 2:
                tvInsMode.setText(R.string.ins_three);
                break;
        }
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_balance_calibration;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_confirm:
                start();
                break;
        }
    }

    private void start() {
        switch (step) {
            case 0:
                tvConfirm.setText(R.string.ok);
                tvHint.setText(R.string.balance_calibration_hint_2);
                WriteHelper.write(WriteHelper.AUTOBALANCE + "1");
                step += 1;
                break;
            case 1:
                deviceStatus.setSetValue(balanceDifference);
                tvConfirm.setText(R.string.quit);
                tvHint.setText(R.string.balance_calibration_hint_3);
                WriteHelper.write(WriteHelper.AUTOBALANCE + "2");
                step += 1;
                break;
            case 2:
                finish();
                break;
            case 3:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        WriteHelper.write(WriteHelper.AUTOBALANCE + "0");
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (deviceStatus.getInsMode() == 2) {
            tvSetValueUnit.setText("°");
            tvCurrentValueUnit.setText("°");
        }
    }

    @Override
    public void onAnalysisHexLocationData(LocationStatus locationStatus) {
        super.onAnalysisHexLocationData(locationStatus);
        setCurrentBalance(locationStatus);
    }

    @Override
    public void onAnalysisAscDeviceStatus(DeviceStatus deviceStatus) {
        super.onAnalysisAscDeviceStatus(deviceStatus);
        balanceDifference = deviceStatus.getBalanceDifference();
        setSetBalance(balanceDifference);
    }

    private void setCurrentBalance(LocationStatus locationStatus) {
        int insMode = deviceStatus.getInsMode();
        //1两端高差  2平衡角度
        switch (insMode) {
            case 1:
                tvCurrentValue.setText(UnitUtils.HEIGHT, deviceStatus.getHeight(), locationStatus.getAltitude2() - locationStatus.getAltitude());
                break;
            case 2:
                tvCurrentValue.setText(StringUtils.setAccuracy(locationStatus.getElevation(), 3));
                break;
        }
    }

    private void setSetBalance(double value) {
        int insMode = deviceStatus.getInsMode();
        //1两端高差  2平衡角度
        switch (insMode) {
            case 1:
                tvSetValue.setText(UnitUtils.HEIGHT, deviceStatus.getHeight(), value);
                break;
            case 2:
                tvSetValue.setText(StringUtils.setAccuracy(value, 3));
                break;
        }
    }
}
